<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="应用名称">
                <a-input v-model="queryParam.tenantName" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="所属租户">
                <a-input v-model="queryParam.tenantName" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleResetQuery">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
      </div> -->

      <s-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        showPagination="auto">
        <template slot="name" slot-scope="text, record">
          <a @click.prevent="handleDetail(record)">{{ text }}</a>
        </template>

        <span slot="status" slot-scope="text">
          <a-badge class="badge-size-large" :status="text | statusTypeFilter" :text="text | statusTextFilter" />
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <!-- <span class="table-actionItem"><a @click="handleEdit(record)">编辑</a></span>
            <span class="table-actionItem">
              <a-popconfirm :title="$t('resetPwdWarning')" @confirm="handleStatusChange(record)">
                <a>{{ record.status === 0 ? '禁用' : '启用' }}</a>
              </a-popconfirm>
            </span> -->
            <span class="table-actionItem" v-if="record.status === 1">
              <a-popconfirm :title="$t('resetPwdWarning')" @confirm="handleRecover(record)">
                <a>恢复</a>
              </a-popconfirm>
            </span>
            <!-- <span class="table-actionItem">
              <a-popconfirm
                :title="$t('delWarning', { name: $t('tenant.tenant').toLowerCase() })"
                @confirm="handleDelete(record.id)"
              >
                <a class="link-danger">删除</a>
              </a-popconfirm>
            </span> -->
          </template>
        </span>
      </s-table>

      <!-- <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
      <step-by-step-modal ref="modal" @ok="handleOk"/> -->

      <tenant-modal ref="tenantModal" />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
// import { getServiceList } from '@/api/manage'

// import TenantModal from './modules/TenantModal'

// import StepByStepModal from './modules/StepByStepModal'
// import CreateForm from './modules/CreateForm'

const statusMap = {
  0: {
    type: 'success',
    text: '正常'
  },
  1: {
    type: 'error',
    text: '已删除'
  }
}

export default {
  name: 'ApplicationList',
  components: {
    STable
    // TenantModal,
    // CreateForm,
    // StepByStepModal
  },
  data () {
    return {
      queryParam: {},
      loadData: parameter => {
        return Promise.resolve({
          data: [],
          pageSize: 10,
          pageNo: 1,
          totalPage: 1,
          totalCount: 0
        })

        // const requestParameters = Object.assign({}, parameter, this.queryParam)
        // console.log('loadData request parameters:', requestParameters)
        // return getServiceList(requestParameters).then((res) => {
        //   return res.result
        // })
      }
    }
  },
  computed: {
    columns () {
      const columns = [
        {
          title: '应用名称',
          dataIndex: 'no',
          ellipsis: true,
          scopedSlots: { customRender: 'name' }
        },
        {
          title: '类型',
          dataIndex: 'description',
          ellipsis: true
        },
        {
          title: '所属租户',
          dataIndex: 'callNo3',
          ellipsis: true
        },
        {
          title: '备注',
          dataIndex: 'callNo1',
          ellipsis: true
        },
        {
          title: '创建人',
          dataIndex: 'callNo2',
          ellipsis: true
        },
        {
          title: '创建时间',
          dataIndex: 'callNo3',
          width: 200,
          customRender: () => '2021-03-12 16:02:01'
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: 120,
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: 120,
          scopedSlots: { customRender: 'action' }
        }
      ]
      return columns
    }
  },
  filters: {
    statusTextFilter (type) {
      return (statusMap[type] || {}).text || '--'
    },
    statusTypeFilter (type) {
      return (statusMap[type] || {}).type || 'default'
    }
  },
  created () {
    // getRoleList({ t: new Date() })
  },
  methods: {
    handleOk () {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('values', values)
          if (values.id > 0) {
            // 修改 e.g.
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.info('修改成功')
            })
          } else {
            // 新增
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.info('新增成功')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleSub (record) {
      if (record.status !== 0) {
        this.$message.info(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    },
    // resetSearchForm () {
    //   this.queryParam = {
    //     date: moment(new Date())
    //   }
    // },

    handleResetQuery () {
      this.queryParam = {}
      this.$refs.table.refresh(true)
    },

    handleAdd () {
      this.$refs.tenantModal.add()
    },
    handleEdit (record) {
      this.$refs.tenantModal.edit(record)
    },

    handleDetail (record) {
      // this.$router.push({ name: 'tenantDetail', query: { id: record.id } })
    },

    handleStatusChange (record) {},
    handleRecover (record) {},
    handleResetPwd (record) {}
  }
}
</script>
